#include "process_launcher.h"

ProcessLauncher::ProcessLauncher() : StdMarshalInfo(true)
{
	CORE_LOG(L6, (_T("[ProcessLauncher::ProcessLauncher]")));
}

ProcessLauncher::~ProcessLauncher()
{
	CORE_LOG(L6, (_T("[ProcessLauncher::~ProcessLauncher]")));
}

STDMETHODIMP ProcessLauncher::LaunchCmdLine(const TCHAR* cmd_line)
{
	CORE_LOG(L1, (_T("[ProcessLauncher::LaunchCmdLine][%s]"), cmd_line));

	// The exception barrier is needed, because any exceptions that are thrown
	// in this method will get caught by the COM run time. We compile with
	// exceptions off, and do not expect to throw any exceptions. This barrier
	// will treat an exception in this method as a unhandled exception.
	ExceptionBarrier barrier;
	if (cmd_line == NULL)
		return E_INVALIDARG;

	// http://b/3329538: In the impersonated case, need to create a fresh
	// environment block and ::CreateProcess. RunAsCurrentUser does just that.
	HRESULT hr = vista::RunAsCurrentUser(cmd_line);
	if (FAILED(hr))
		UTIL_LOG(LW, (_T("[RunAsCurrentUser failed][0x%x]"), hr));

	return hr;
}

STDMETHODIMP ProcessLauncher::LaunchBrowser(DWORD type, const TCHAR* url)
{
	CORE_LOG(L1, (_T("[ProcessLauncher::LaunchBrowser][%d][%s]"), type, url));
	// The exception barrier is needed, because any exceptions that are thrown
	// in this method will get caught by the COM run time. We compile with
	// exceptions off, and do not expect to throw any exceptions. This barrier
	// will treat an exception in this method as a unhandled exception.
	ExceptionBarrier barrier;
	if (type >= BROWSER_MAX || url == NULL)
		return E_INVALIDARG;

	return RunBrowser(static_cast<BrowserType>(type), url);
}